﻿Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports Ionic.Zip

Public Class Bank

	Public Shared Function GetAll() As List(Of Entity.BankItem)
		Return DL.Bank.AllBank
	End Function

	Public Shared Function ExportReportBBL(ByVal month As String, ByVal year As String, ByVal rptPath As String, ByVal zipPath As String, ByVal outPutSteam As IO.Stream, ByVal incomeForm As String, ByVal incomeTo As String) As Boolean
		Return ExportReportBBL(month, year, rptPath, zipPath, outPutSteam, incomeForm, incomeTo, "'NM'")
	End Function

	Public Shared Function ExportReportBBL_605(ByVal month As String, ByVal year As String, ByVal rptPath As String, ByVal zipPath As String, ByVal outPutSteam As IO.Stream, ByVal incomeForm As String, ByVal incomeTo As String) As Boolean
		Return ExportReportBBL(month, year, rptPath, zipPath, outPutSteam, incomeForm, incomeTo, BL.SystemConfig.GetStatusReport605)
	End Function

	Public Shared Function ExportReportSmart_606(ByVal month As String, ByVal year As String, ByVal rptPath As String, ByVal zipPath As String, ByVal outPutSteam As IO.Stream, ByVal incomeForm As String, ByVal incomeTo As String) As Boolean
		Return ExportReportSmart(month, year, rptPath, zipPath, outPutSteam, incomeForm, incomeTo, BL.SystemConfig.GetStatusReport606)
	End Function

    Public Shared Function ExportReportBBL_607(ByVal month As String, ByVal year As String, ByVal rptPath As String, ByVal zipPath As String, ByVal outPutSteam As IO.Stream, ByVal incomeForm As String, ByVal incomeTo As String) As Boolean
        Return ExportReportBBL(month, year, rptPath, zipPath, outPutSteam, incomeForm, incomeTo, BL.SystemConfig.GetStatusReport607)
    End Function

    Public Shared Function ExportReportSmart_608(ByVal month As String, ByVal year As String, ByVal rptPath As String, ByVal zipPath As String, ByVal outPutSteam As IO.Stream, ByVal incomeForm As String, ByVal incomeTo As String) As Boolean
        Return ExportReportSmart(month, year, rptPath, zipPath, outPutSteam, incomeForm, incomeTo, BL.SystemConfig.GetStatusReport608)
    End Function

	Public Shared Function ExportReportSmart(ByVal month As String, ByVal year As String, ByVal rptPath As String, ByVal zipPath As String, ByVal outPutSteam As IO.Stream, ByVal incomeForm As String, ByVal incomeTo As String) As Boolean
		Return ExportReportSmart(month, year, rptPath, zipPath, outPutSteam, incomeForm, incomeTo, "'NM'")
	End Function

	Public Shared Function ExportReportBBL(ByVal month As String, ByVal year As String, ByVal rptPath As String, ByVal zipPath As String, ByVal outPutSteam As IO.Stream, ByVal incomeForm As String, ByVal incomeTo As String, ByVal officerStatus As String) As Boolean
		Dim dt As DataTable
		Dim ds As DataSet

		Try
			dt = DL.Report.DataUT603(month, year, incomeForm, incomeTo, officerStatus)
		Catch ex As Exception
			Throw New Exception("ไม่สามารถดึงข้อมูลจากฐานข้อมูลได้", ex)
		End Try

		'dt.WriteXmlSchema("C:\Documents and Settings\sarayut.se\Desktop\SourceCode\UT603.xsd")
		Try
			ds = Common.Utility.SplitDataTable(dt, 60000)
		Catch ex As Exception
			Throw New Exception("ไม่สามารถทำการ split ข้อมูลใน DataTable ได้", ex)
		End Try

		Dim i As Integer = 1
		Dim strFileName As String = Nothing
		Dim strListFileName As New List(Of String)

		Try
			For Each dtExport As DataTable In ds.Tables
				Dim cryRpt As New ReportDocument
				cryRpt.Load(rptPath)
				cryRpt.SetDataSource(dtExport)

				strFileName = "iCaseBBL_" & Today.ToString("yyyyMMdd") & "_" & i.ToString

				Dim CrExportOptions As ExportOptions
				Dim CrDiskFileDestinationOptions As New DiskFileDestinationOptions()
				Dim CrFormatTypeOptions As New ExcelFormatOptions
				CrDiskFileDestinationOptions.DiskFileName = zipPath & strFileName & ".xls"
				CrExportOptions = cryRpt.ExportOptions
				With CrExportOptions
					.ExportDestinationType = ExportDestinationType.DiskFile
					.ExportFormatType = ExportFormatType.ExcelRecord
					.DestinationOptions = CrDiskFileDestinationOptions
					.FormatOptions = CrFormatTypeOptions
				End With

				strListFileName.Add(CrDiskFileDestinationOptions.DiskFileName)

				i += 1

				cryRpt.Export()
				cryRpt.Dispose()
				cryRpt = Nothing
			Next
		Catch ex As Exception
			Throw New Exception("ไม่สามารถทำการ Export ข้อมูลเป็นไฟล์ Excel ได้", ex)
		End Try

		If strListFileName.Count > 0 Then

			Try
				Using zip As New ZipFile()
					zip.AddFiles(strListFileName, "")
					zip.Save(outPutSteam)
				End Using
			Catch ex As Exception
				Throw New Exception("ไม่สามารถทำการ zip ไฟล์ได้", ex)
			End Try

			Try
				For Each strFile As String In strListFileName
					IO.File.Delete(strFile)
				Next
			Catch ex As Exception
				Throw New Exception("ไม่สามารถลบไฟล์ที่ได้จากการ Export ได้", ex)
			End Try

			Return True
		Else
			'แสดงว่่าไม่มีไฟล์ที่ export ได้
			Return False
		End If

	End Function

	Public Shared Function ExportReportSmart(ByVal month As String, ByVal year As String, ByVal rptPath As String, ByVal zipPath As String, ByVal outPutSteam As IO.Stream, ByVal incomeForm As String, ByVal incomeTo As String, ByVal officerStatus As String) As Boolean
		Dim dt As DataTable
		Dim ds As DataSet

		Try
			dt = DL.Report.DataUT604(month, year, incomeForm, incomeTo, officerStatus)
		Catch ex As Exception
			Throw New Exception("ไม่สามารถดึงข้อมูลจากฐานข้อมูลได้", ex)
		End Try

		Try
			ds = Common.Utility.SplitDataTable(dt, 997)
		Catch ex As Exception
			Throw New Exception("ไม่สามารถทำการ split ข้อมูลใน DataTable ได้", ex)
		End Try

		Dim i As Integer = 1
		Dim strFileName As String = Nothing
		Dim strListFileName As New List(Of String)

		Try
			For Each dtExport As DataTable In ds.Tables
				Dim cryRpt As New ReportDocument
				cryRpt.Load(rptPath)
				cryRpt.SetDataSource(dtExport)

				strFileName = "iCaseSmart_" & Today.ToString("yyyyMMdd") & "_" & i.ToString

				Dim CrExportOptions As ExportOptions
				Dim CrDiskFileDestinationOptions As New DiskFileDestinationOptions()
				Dim CrFormatTypeOptions As New ExcelFormatOptions
				CrDiskFileDestinationOptions.DiskFileName = zipPath & strFileName & ".xls"
				CrExportOptions = cryRpt.ExportOptions
				With CrExportOptions
					.ExportDestinationType = ExportDestinationType.DiskFile
					.ExportFormatType = ExportFormatType.ExcelRecord
					.DestinationOptions = CrDiskFileDestinationOptions
					.FormatOptions = CrFormatTypeOptions
				End With

				strListFileName.Add(CrDiskFileDestinationOptions.DiskFileName)

				i += 1

				cryRpt.Export()
				cryRpt.Dispose()
				cryRpt = Nothing
			Next
		Catch ex As Exception
			Throw New Exception("ไม่สามารถทำการ Export ข้อมูลเป็นไฟล์ Excel ได้", ex)
		End Try

		If strListFileName.Count > 0 Then

			Try
				Using zip As New ZipFile()
					zip.AddFiles(strListFileName, "")
					zip.Save(outPutSteam)
				End Using
			Catch ex As Exception
				Throw New Exception("ไม่สามารถทำการ zip ไฟล์ได้", ex)
			End Try

			Try
				For Each strFile As String In strListFileName
					IO.File.Delete(strFile)
				Next
			Catch ex As Exception
				Throw New Exception("ไม่สามารถลบไฟล์ที่ได้จากการ Export ได้", ex)
			End Try

			Return True
		Else
			'แสดงว่่าไม่มีไฟล์ที่ export ได้
			Return False
		End If

	End Function

End Class
